# -*- python -*-
# ex: set syntax=python:

# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# It has one job: define a dictionary named BuildmasterConfig. This
# dictionary has a variety of keys to control different aspects of the
# buildmaster. They are documented in docs/config.xhtml .

from buildbot.changes import filter
from buildbot.config import BuilderConfig
from buildbot.schedulers.basic import SingleBranchScheduler
from buildbot.status.mail import MailNotifier
from master import slaves_list
from master import master_utils
from master.factory import annotator_factory
from master.repo_poller import RepoPoller

import master_site_config
import config

c = BuildmasterConfig = {}


ActiveMaster = master_site_config.ART

art_recipe_factory=annotator_factory.AnnotatorFactory().BaseFactory(recipe='art')

builders_config = {
  '1aosp-host-x86': [
    'host-x86-ndebug',
    'host-x86-debug',
  ],
  '1aosp-host-x86_64': [
    'host-x86_64-ndebug',
    'host-x86_64-debug',
  ],
  '1aosp-target-armv7': [
    'angler-armv7-ndebug',
    'angler-armv7-debug',
  ],
  '1aosp-target-armv8': [
    'angler-armv8-ndebug',
    'angler-armv8-debug',
  ],
  '1aosp-target-fugu': [
    'fugu-ndebug',
    'fugu-debug',
  ],
  '2aosp-cms': [
    'host-x86-cms',
    'host-x86_64-cms',
    'angler-armv7-cms',
    'angler-armv8-cms',
  ],
  '3aosp-builder': [
    'aosp-builder-cms',
    'aosp-builder-cc',
  ],
  '4aosp-poison': [
    'host-x86-poison-debug',
    'host-x86_64-poison-debug',
    'volantis-armv7-poison-debug',
    'volantis-armv8-poison-ndebug',
    'volantis-armv8-poison-debug',
  ],
  '5aosp-valgrind': [
    'host-x86-valgrind',
    'host-x86_64-valgrind',
  ],
}

# Builders setup.
# We reboot for valgrind as some processes sometimes become stalled.
c['builders'] = []
for category, builders in builders_config.iteritems():
  for builder in builders:
    builder_dict = {
      'name': builder,
      'category': category,
      'factory': art_recipe_factory,
      'auto_reboot': category == '5aosp-valgrind',
    }
    c['builders'].append(builder_dict)

repopoller = RepoPoller(
    repo_url = 'https://android.googlesource.com/platform',
    manifest = 'manifest',
    repo_branches = ['master-art'],
    pollInterval = 300,
    revlinktmpl = 'https://android.googlesource.com/platform/%s/+/%s')
c['change_source'] = repopoller

# Always merge build requests to keep up on changes from all our
# dependencies.
c['mergeRequests'] = lambda *_: True

first_builders = ['host-x86-ndebug',
                  'host-x86-debug',
                  'host-x86-cms',
                  'host-x86-poison-debug',
                  'host-x86-valgrind',
                  'host-x86_64-ndebug',
                  'host-x86_64-debug',
                  'host-x86_64-cms',
                  'host-x86_64-poison-debug',
                  'host-x86_64-valgrind',
                  'angler-armv7-ndebug',
                  'volantis-armv8-poison-ndebug',
                  'angler-armv8-ndebug',
                  'fugu-ndebug',
                  'aosp-builder-cms',
                  'aosp-builder-cc']

second_builders = ['angler-armv7-debug',
                   'volantis-armv8-poison-debug',
                   'angler-armv8-debug',
                   'fugu-debug']

third_builders = ['angler-armv7-cms',
                  'volantis-armv7-poison-debug',
                  'angler-armv8-cms']

# Scheduler setup: we create three schedulers to workaround
# recipes and max_steps > 1 incompatibilities.
# https://bugs.chromium.org/p/chromium/issues/detail?id=650169
first_trunkchanged = SingleBranchScheduler(
    name = 'first_trunkchanged',
    change_filter = filter.ChangeFilter(branch = 'master-art'),
    treeStableTimer = 0,
    builderNames = first_builders)

second_trunkchanged = SingleBranchScheduler(
    name = 'second_trunkchanged',
    change_filter = filter.ChangeFilter(branch = 'master-art'),
    treeStableTimer = 60,
    builderNames = second_builders)

third_trunkchanged = SingleBranchScheduler(
    name = 'third_trunkchanged',
    change_filter = filter.ChangeFilter(branch = 'master-art'),
    treeStableTimer = 120,
    builderNames = third_builders)

c['schedulers'] = [ first_trunkchanged,
                    second_trunkchanged,
                    third_trunkchanged ]


# Slave setup
slaves = slaves_list.SlavesList('slaves.cfg', 'ART')
for builder in c['builders']:
  # Associate the slaves to the builders. The configuration is in slaves.cfg.
  builder['slavenames'] = slaves.GetSlavesName(builder=builder['name'])


c['slaves'] = master_utils.AutoSetupSlaves(
    c['builders'],
    config.Master.GetBotPassword(),
    max_builds=3)


# Adds common status and tools to this master.
master_utils.AutoSetupMaster(c, ActiveMaster,
    public_html='../master.chromium/public_html',
    templates=['./templates', '../master.chromium/templates'],
    order_console_by_time=True)

mail_builders = ['host-x86-ndebug',
                 'host-x86-debug',
                 'host-x86-cms',
                 'host-x86-poison-debug',
                 'host-x86_64-ndebug',
                 'host-x86_64-debug',
                 'host-x86_64-cms',
                 'host-x86_64-poison-debug',
                 'host-x86-valgrind',
                 'host-x86_64-valgrind',
                 'angler-armv7-ndebug',
                 'angler-armv7-debug',
                 'angler-armv7-cms',
                 'volantis-armv7-poison-debug',
                 'volantis-armv8-poison-ndebug',
                 'volantis-armv8-poison-debug',
                 'angler-armv8-ndebug',
                 'angler-armv8-debug',
                 'angler-armv8-cms',
                 'fugu-ndebug',
                 'fugu-debug',
                 'aosp-builder-cms',
                 'aosp-builder-cc']

if ActiveMaster.is_production_host:
  c['status'].append(MailNotifier(
      fromaddr=ActiveMaster.from_address,
      sendToInterestedUsers=False,
      relayhost=config.Master.smtp,
      mode='problem',
      builders=mail_builders,
      extraRecipients=['ngeoffray@google.com',
                       'agampe@google.com',
                       'dalvik-team+chromium-buildbot@google.com']))

####### PROJECT IDENTITY

c['projectName'] = ActiveMaster.project_name
c['projectURL'] = config.Master.project_url
c['buildbotURL'] = ActiveMaster.buildbot_url
